package tree;

import offer.TreeNode;

public class leetCode1339_maxProduct {
    int allSum = 0;
    int ans = 0;
    public int maxProduct(TreeNode root) {
        allSum = sum(root);
        build(root);
        return (int)(ans % (1e9+7));
    }

    public int sum(TreeNode root){
        if(root == null) return 0;
        return sum(root.left) + sum(root.right) + root.val;
    }
    public int build(TreeNode root){
        if(root == null) return 0;
        int left = build(root.left);
        int right = build(root.right);
        int sumTmp = root.val + left + right;
        ans = Math.max(ans,(allSum - sumTmp) * sumTmp);
        return sumTmp;

    }

}
